package global;

import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import android.graphics.Bitmap;
import android.graphics.Color;

public class Util {
	public static double[] toData(Bitmap bm, int size) throws Exception
	{
		if(bm.getHeight()!=size||bm.getWidth()!=size)
			throw new Exception("Image size must be "+size+"*"+size);
		
		List<Double> Data = new ArrayList<Double>();
		
        double[][] temp=new double[size][size];
        int pixel;
        double total_R=0, total_G=0, total_B=0, total_temp=0;
		int r,g,b;
		
        for (int i = 0; i < size; i++){
            for (int j = 0; j < size; j++){
            	pixel= bm.getPixel(i, j);
            	
            	r=(int)Color.red(pixel);
            	g=(int)Color.green(pixel);
            	b=(int)Color.blue(pixel);
            	
                total_R += r;
                total_G += g;
                total_B += b;

                temp[i][j] = r * 0.299 + g * 0.587 + b * 0.114;
                total_temp += temp[i][j];
            }
        }
        
      //Calculate the average value;
        total_R = (total_R / (size*size)) / 256; Data.add(total_R);
        total_G = (total_G / (size*size)) / 256; Data.add(total_G);
        total_B = (total_B / (size*size)) / 256; Data.add(total_B);

        double Threshold=(total_temp / (size*size));
        
      //Calculate the horizontal parameters

        for (int i = 0; i < size; i++){
            total_temp = 0;
            for (int j = 0; j < size; j++)
                if (temp[i][j] > Threshold) total_temp += temp[i][j];
            Data.add(total_temp / (size));
        }
     /* for (int i = 0; i < size; i++){
            total_temp = 0;
            for (int j = size/2; j < size; j++){
            	if (temp[i][j] > Threshold) total_temp += temp[i][j];
            }
            Data.add(total_temp/(size/2));
        }
        /*for (int i = 0; i < size; i++){
            total_temp = 0;
            int t = i;
            for (int j = 0; j < size - t; j++){
                if (temp[t][j] > Threshold) total_temp += temp[t][j];
                t++;
            }
            Data.add(total_temp/t);
        }
        /*for (int i = 29; i >= 0; i--){
            total_temp = 0;
            int t = i;
            for (int j = 0; j <= t; j++){
                if (temp[t][j] > Threshold) total_temp += temp[t][j];
                t--;
            }
            Data.add(total_temp/t);
        }*/
        
      //Calculate the vertical parameters

       for (int j = 0; j < size; j++){
            total_temp = 0;
            for (int i = 0; i < size; i++)
                if (temp[i][j] > Threshold) total_temp += temp[i][j];
            Data.add(total_temp / (size));
        }
        /*for (int j = 0; j < size; j++){
            total_temp = 0;
            for (int i = size/2; i < size; i++){
            	if (temp[i][j] > Threshold)  total_temp += temp[i][j];
            }
            Data.add(total_temp/(size/2));
        }
        
        /*for (int j = 0; j < size; j++){
            total_temp = 0;
            int t = j;
            for (int i = 0; i < size - t; i++){
                if (temp[i][t] > Threshold) total_temp += temp[i][t];
                t++;
            }
            Data.add(total_temp/t);
        }
        /*for (int j = 29; j >= 0; j--){
            total_temp = 0;
            int t = j;
            for (int i = 0; i <= t; i++){
                if (temp[i][t] > Threshold) total_temp += temp[i][t];
                t--;
            }
            Data.add(total_temp/t);
        }*/
        /*for(int i = 0; i < size; i++){
        	for(int j = 0; j < size; j++){
        		if (temp[i][j] > Threshold)
        			Data.add(temp[i][j]);
        		else
        			Data.add(0.0);
        	}
        }*/
        
		double result[]=new double[Data.size()];
		for (int i = 0; i < Data.size(); i++)
        {
            result[i] = (double)Data.get(i);
        }
		
		return result;
	}
	
	public static Document readXml(InputStream is) throws SAXException, IOException,
	    ParserConfigurationException {
	    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
	
	    dbf.setValidating(false);
	    dbf.setIgnoringComments(false);
	    dbf.setIgnoringElementContentWhitespace(true);
	    dbf.setNamespaceAware(true);
	    // dbf.setCoalescing(true);
	    // dbf.setExpandEntityReferences(true);
	
	    DocumentBuilder db = null;
	    db = dbf.newDocumentBuilder();
	    db.setEntityResolver(new NullResolver());
	
	    // db.setErrorHandler( new MyErrorHandler());
	
	    return db.parse(is);
	}
}

class NullResolver implements EntityResolver {
	  public InputSource resolveEntity(String publicId, String systemId) throws SAXException,
	      IOException {
	    return new InputSource(new StringReader(""));
	  }
}
